package Frame;

import Frame.entity.EditMenuButton;
import Frame.entity.SingleJMenuBar;
import Function.*;
import VWorldGameStudio.FCMap;
import VWorldGameStudio.FCMapAnimation;
import io.reactivex.schedulers.Schedulers;
import lombok.Getter;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 游戏编辑器的核心类
 *
 * @author jungle
 */
public class FCGMaster extends JFrame {
    // 窗体图标
    private Image iconImage = Toolkit.getDefaultToolkit().getImage("./image/V-Word Game Studio for bilibili.png");
    // 窗体标题
    private String title = "FC地图编辑器 V1.0";
    // 获取当前屏幕尺寸
    private Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize();
    // TODO 这是个啥东西
    private JCheckBox[] cf = new JCheckBox[4];
    // TODO 这又是啥
    private int selectCF = -1;
    // TODO 这个呢
    private JFileChooser filec = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()) {
        protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
            JDialog jd = super.createDialog(parent);
            jd.setSize(500, 400);
            jd.setTitle("选择文件");
            jd.setIconImage(iconImage);
            jd.setResizable(false);
            jd.setLocationRelativeTo(null);
            return jd;
        }

        ;
    };

    // TODO 暂未使用，含义不明
    private String selectEntityName = null;
    private FCCorrect correct = new FCCorrect();

    // 各类元素类库
    private FCBlockLibrary blockLibrary;
    public static FCCharacterLibrary characterLibrary;
    public static FCTankLibrary tanksLibrary;
    public static FCItemLibrary itemLibrary;
    public static FCMonsterLibrary monsterLibrary;
    private FCMapLibrary mapLibrary;
    public static FCEventLibrary eventLibrary;
    @Getter
    private static FCAnimationLibrary animationLibrary;
    private static FCActionLibrary actionLibrary;
    public static FCPlotLibrary plotLibrary;

    public static FCMapAnimationLibrary mapAnimationLibrary;

    JComboBox<String> maptbox;

    public void reloadData() {
        if (maptbox != null) {
            maptbox.removeAllItems();
            maptbox.addItem("null");
            for (FCMapAnimation fcMapAnimation : mapAnimationLibrary.getAllAnimations()) {
                maptbox.addItem(fcMapAnimation.getAName());
            }
            maptbox.setSelectedIndex(0);
        }
    }

    /**
     * 窗体的主要内容均由此方法生成
     */
    public FCGMaster() {
        /******************** 窗体的基本信息START ********************/
        setBounds(0, 0, winSize.width, winSize.height);
        setLayout(null);
        setLocationRelativeTo(null);
        setResizable(false);
        setTitle(title);
        setIconImage(iconImage);
        /******************** 窗体的基本信息END ********************/

        /******************** 窗体左侧地图信息区域START ********************/
        // 当前地图基本信息
        int jLabelX = 10;
        int jLabelY = 10;
        int jLabelWidth = 60;
        int jLabelHeight = 20;
        JLabel mapName = new JLabel("地图名称");
        mapName.setBounds(jLabelX, jLabelY, jLabelWidth, jLabelHeight);
        this.add(mapName);
        JTextField mapNameText = new JTextField();
        mapNameText.setBounds(70, 10, 140, 20);
        this.add(mapNameText);

        JLabel mapSize = new JLabel("地图尺寸");
        mapSize.setBounds(jLabelX, jLabelY + 30, jLabelWidth, jLabelHeight);
        this.add(mapSize);
        JLabel mapSizeText = new JLabel();
        mapSizeText.setBounds(70, 40, 140, 20);
        this.add(mapSizeText);

        JLabel mapInfo = new JLabel("地图描述");
        mapInfo.setBounds(jLabelX, jLabelY + 60, jLabelWidth, jLabelHeight);
        this.add(mapInfo);
        JTextArea mapInfoTextArea = new JTextArea();
        mapInfoTextArea.setBounds(10, 100, 200, 200);
        this.add(mapInfoTextArea);

        /******************** 窗体左侧地图信息区域END ********************/

        /******************** TODO 窗体中间地图显示区域START ********************/
        // 窗体中间那一片在哪
        /******************** TODO 窗体中间地图显示区域END ********************/

        /******************** 窗体右侧素材选择区START ********************/
        JLabel pst = new JLabel("素材选定");
        pst.setBounds(winSize.width - 210, 10, 100, 20);
        this.add(pst);

        PngSelectJpanel psj = new PngSelectJpanel(winSize.width - 210, 40, 200, winSize.height - 100);
        blockLibrary = new FCBlockLibrary(false, psj);
        characterLibrary = new FCCharacterLibrary();
        tanksLibrary = new FCTankLibrary(false);
        itemLibrary = new FCItemLibrary(false);
        monsterLibrary = new FCMonsterLibrary();
        monsterLibrary.loadMonsterData();
        mapLibrary = new FCMapLibrary(false);
        eventLibrary = new FCEventLibrary();
        eventLibrary.loadEventData();
        actionLibrary = new FCActionLibrary();
        actionLibrary.loadActionData();
        animationLibrary = new FCAnimationLibrary();
        animationLibrary.loadAnimationData();
        plotLibrary = new FCPlotLibrary(false);
        mapAnimationLibrary = new FCMapAnimationLibrary();
        mapAnimationLibrary.loadAimationData();
        psj.loadBlockPngAndName(blockLibrary);
        buildSmallMapImg();
        MapEditJpanel mapEditJpanel = new MapEditJpanel(220, 10, winSize.width - 440, winSize.height - 70, blockLibrary);
        psj.setMapEditJpanel(mapEditJpanel);
        this.add(psj);
        this.add(mapEditJpanel);

        /******************** 窗体右侧素材选择区END ********************/

        /******************** 窗体左侧信息区域START ********************/

        // 设置地图中事件触发点的触发类型选择区
        JLabel cfwb = new JLabel("触发事件选择");
        cfwb.setBounds(10, 310, 80, 20);
        this.add(cfwb);
        JButton cfwb1 = new JButton("设置");
        cfwb1.setBounds(140, 310, 70, 20);
        this.add(cfwb1);
        cfwb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCMapEditEvent(eventLibrary, mapEditJpanel);
            }
        });

        JLabel mapt = new JLabel("地图动画选择");
        mapt.setBounds(10, 340, 80, 20);
        this.add(mapt);
        maptbox = new JComboBox<String>();
        maptbox.setBounds(100, 340, 110, 20);
        maptbox.addItem("null");
        for (FCMapAnimation fcMapAnimation : mapAnimationLibrary.getAllAnimations()) {
            maptbox.addItem(fcMapAnimation.getAName());
        }
        this.add(maptbox);

        maptbox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mapEditJpanel.setSelecAnimationName((String) maptbox.getSelectedItem());
            }
        });

        // 地图编辑以及显示区域
        JLabel a = new JLabel("地图跳转至");
        a.setBounds(10, 400, 70, 20);
        this.add(a);
        JLabel a1 = new JLabel("X:");
        a1.setBounds(10, 430, 20, 20);
        this.add(a1);
        JTextField a2 = new JTextField();
        a2.setBounds(30, 430, 70, 20);
        this.add(a2);
        JLabel a3 = new JLabel("Y:");
        a3.setBounds(120, 430, 20, 20);
        this.add(a3);
        JTextField a4 = new JTextField();
        a4.setBounds(140, 430, 70, 20);
        this.add(a4);
        JButton a5 = new JButton("跳转");
        a5.setBounds(140, 400, 70, 20);
        this.add(a5);
        mapEditJpanel.setJTextFieldOfXY(a2, a4);
        // 人物添加选择
        JLabel b = new JLabel("人物选择");
        b.setBounds(10, 460, 70, 20);
        this.add(b);
        JComboBox<String> b1 = new JComboBox<String>();
        b1.setBounds(10, 490, 200, 20);
        b1.addItem("null");
        for(String name: characterLibrary.characterName){
            b1.addItem(name);
        }

        this.add(b1);
        JButton b2 = new JButton("添加");
        b2.setBounds(140, 460, 70, 20);
        this.add(b2);
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String npcname = b1.getSelectedItem().toString();
                if (npcname.indexOf("player") > 1)
                    npcname = "null";
                mapEditJpanel.setSelectNpcName(npcname);
            }
        });

        // 坦克添加选择
        JLabel c = new JLabel("坦克选择");
        c.setBounds(10, 520, 70, 20);
        this.add(c);
        JComboBox<String> c1 = new JComboBox<String>();
        c1.setBounds(10, 550, 200, 20);
        c1.addItem("null");
        for (int i = 0; i < tanksLibrary.getNewTankID(); i++)
            c1.addItem(tanksLibrary.getTankNameOfID(i));
        this.add(c1);
        JButton c2 = new JButton("添加");
        c2.setBounds(140, 520, 70, 20);
        this.add(c2);
        c2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String tankname = c1.getSelectedItem().toString();
                mapEditJpanel.setSelectTankName(tankname);
            }
        });

        // 怪物区域设置
        JLabel d = new JLabel("怪物区域");
        d.setBounds(10, 580, 70, 20);
        this.add(d);
//		JComboBox<String> d1 = new JComboBox<String>();
//		d1.setBounds(10, 610, 200, 20);
//		d1.addItem("null");
//		for (int i = 0; i < monsterLibrary.getMonsterNewID(); i++)
//			d1.addItem(monsterLibrary.getMonsterNameOfNum(i));
//		this.add(d1);
        JButton d2 = new JButton("添加");
        d2.setBounds(140, 580, 70, 20);
        this.add(d2);
        d2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (mapEditJpanel.getNewMap() != null) {
                    new FCEditMonsterRelateMap(true, mapEditJpanel.getNewMap(), mapEditJpanel);
                }
            }
        });
        JButton d22 = new JButton("编辑");
        d22.setBounds(140, 600, 70, 20);
        this.add(d22);
        d22.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (mapEditJpanel.getNewMap() != null) {
                    new FCEditMonsterRelateMap(false, mapEditJpanel.getNewMap(), mapEditJpanel);
                }
            }
        });

        JLabel tips1 = new JLabel("提示1:右键是拖动地图");
        tips1.setBounds(10, 640, 200, 20);
        this.add(tips1);
        JLabel tips2 = new JLabel("提示2:添加图块要左键选择完后再在");
        tips2.setBounds(10, 660, 250, 20);
        this.add(tips2);
        JLabel tips22 = new JLabel("地图上左键添加");
        tips22.setBounds(10, 680, 250, 20);
        this.add(tips22);
        JLabel tips3 = new JLabel("提示3:删除图块要左键选择相同图块");
        tips3.setBounds(10, 700, 250, 20);
        this.add(tips3);
        JLabel tips33 = new JLabel("在地图左键点击删除");
        tips33.setBounds(10, 720, 250, 20);
        this.add(tips33);
        JLabel tips4 = new JLabel("提示4:添加事件等是先选择事件再在");
        tips4.setBounds(10, 740, 250, 20);
        this.add(tips4);
        JLabel tips44 = new JLabel("地图左键添加");
        tips44.setBounds(10, 760, 250, 20);
        this.add(tips44);
        JLabel tips5 = new JLabel("提示5:删除事件等在地图啥也不选用");
        tips5.setBounds(10, 780, 250, 20);
        this.add(tips5);
        JLabel tips55 = new JLabel("左键点击对应地图删除");
        tips55.setBounds(10, 800, 250, 20);
        this.add(tips55);
        /******************** 窗体左侧信息区域END ********************/

        /******************** 窗体上方菜单栏定义START ********************/
        this.setJMenuBar(SingleJMenuBar.getInstance());

        List<String> m1items = new ArrayList<String>(Arrays.asList("新建地图", "保存地图", "删除地图", "打开地图", "地图重制"));
        EditMenuButton jMenu1 = new EditMenuButton("文件", m1items);
        List<String> m2items = new ArrayList<String>(Arrays.asList("添加方块", "编辑方块", "删除方块"));
        EditMenuButton jMenu2 = new EditMenuButton("方块管理", m2items);
        List<String> m3items = new ArrayList<String>(Arrays.asList("添加物品", "编辑物品", "删除物品"));
        EditMenuButton jMenu3 = new EditMenuButton("物品管理", m3items);
        List<String> m4items = new ArrayList<String>(Arrays.asList("添加人物", "编辑人物", "删除人物", "查看人物"));
        EditMenuButton jMenu4 = new EditMenuButton("人物管理", m4items);
        List<String> m5items = new ArrayList<String>(Arrays.asList("查看怪物", "添加怪物", "编辑怪物", "删除怪物"));
        EditMenuButton jMenu5 = new EditMenuButton("怪物管理", m5items);
        List<String> m6items = new ArrayList<String>(Arrays.asList("查看坦克", "添加坦克", "编辑坦克", "删除坦克"));
        EditMenuButton jMenu6 = new EditMenuButton("坦克管理", m6items);
        List<String> m7items = new ArrayList<String>(Arrays.asList("查看事件", "新增事件", "编辑事件"));
        EditMenuButton jMenu7 = new EditMenuButton("事件管理", m7items);
        List<String> m8items = new ArrayList<String>(Arrays.asList("查看行为", "添加行为", "编辑行为"));
        EditMenuButton jMenu8 = new EditMenuButton("行为管理", m8items);
        List<String> m9items = new ArrayList<String>(Arrays.asList("查看动画", "添加动画", "编辑动画"));
        EditMenuButton jMenu9 = new EditMenuButton("攻击动画", m9items);
        List<String> m10items = new ArrayList<String>(Arrays.asList("查看剧情", "添加剧情", "编辑剧情"));
        EditMenuButton jMenu10 = new EditMenuButton("剧情系统", m10items);
        List<String> m11items = new ArrayList<String>(Arrays.asList("查看地图动画", "添加地图动画", "编辑地图动画"));
        EditMenuButton jMenu11 = new EditMenuButton("地图动画", m11items);

        /******************** 窗体上方菜单栏定义END ********************/

        /******************** 窗体上方菜单栏监听事件START ********************/
        // 文件 -> 新建地图
        jMenu1.getjMenuItem(0).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCNewMap(mapLibrary, mapEditJpanel, mapNameText, mapSizeText, mapInfoTextArea);
            }
        });
        // 文件 -> 保存地图
        jMenu1.getjMenuItem(1).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCSaveMap(mapEditJpanel.getNewMap(), mapLibrary, mapEditJpanel);
            }
        });
        // 文件 -> 删除地图
        jMenu1.getjMenuItem(2).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCDeleteMap(mapLibrary);
            }
        });
        // 文件 -> 打开地图
        jMenu1.getjMenuItem(3).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCOpenMap(mapLibrary, mapEditJpanel, mapNameText, mapSizeText, mapInfoTextArea);
            }
        });
        // 文件 -> 地图重制
        jMenu1.getjMenuItem(4).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCProgressBar("地图检修", mapEditJpanel, blockLibrary);
            }
        });

        // 方块管理 -> 添加方块
        jMenu2.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditBlock(false, blockLibrary, FCGMaster.this);
            }
        });
        // 方块管理 -> 编辑方块
        jMenu2.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditBlock(true, blockLibrary, FCGMaster.this);
            }
        });
        // 方块管理 -> 删除方块
        jMenu2.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCDeleteBlock(blockLibrary);
            }
        });
        // 物品管理 -> 添加物品
        jMenu3.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //new FCEditItem(true, itemLibrary, eventLibrary, animationLibrary);
            }
        });
        // 物品管理 -> 编辑物品
        jMenu3.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //new FCEditItem(false, itemLibrary, eventLibrary, animationLibrary);
            }
        });
        // 物品管理 -> 删除物品
        jMenu3.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //	new FCDeleteItem();
            }
        });
        // 人物管理 -> 添加人物
        jMenu4.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditCharacter(true, characterLibrary, actionLibrary, eventLibrary);
            }
        });
        // 人物管理 -> 编辑人物
        jMenu4.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditCharacter(false, characterLibrary, actionLibrary, eventLibrary);
            }
        });
        // 人物管理 -> 删除人物
        jMenu4.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCDeleteCharacter();
            }
        });
        // 人物管理 -> 查看人物
        jMenu4.getjMenuItem(3).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCCharacterTable(characterLibrary);
            }
        });
        // 怪物管理 -> 查看怪物
        jMenu5.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCTableMonster(monsterLibrary, iconImage);
            }
        });
        // 怪物管理 -> 添加怪物
        jMenu5.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //	new FCEditMonster(true, monsterLibrary, animationLibrary, itemLibrary);
            }
        });
        // 怪物管理 -> 编辑怪物
        jMenu5.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //	new FCEditMonster(false, monsterLibrary, animationLibrary, itemLibrary);
            }
        });
        // 怪物管理 -> 删除怪物
        jMenu5.getjMenuItem(3).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCDeleteMonster();
            }
        });
        // 坦克管理 -> 查看坦克
        jMenu6.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCTankAndActionTable(tanksLibrary, itemLibrary);
            }
        });
        // 坦克管理 -> 添加坦克
        jMenu6.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditTank(true, tanksLibrary, itemLibrary);
            }
        });
        // 坦克管理 -> 编辑坦克
        jMenu6.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditTank(false, tanksLibrary, itemLibrary);
            }
        });
        // 坦克管理 -> 删除坦克
        jMenu6.getjMenuItem(3).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCDeleteTank(tanksLibrary);
            }
        });
        // 事件管理 -> 查看事件
        jMenu7.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEventAndActionTable(true, eventLibrary, null);
            }
        });
        // 事件管理 -> 新增事件
        jMenu7.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditEvent(true, eventLibrary, mapLibrary, itemLibrary);
            }
        });
        // 事件管理 -> 编辑事件
        jMenu7.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditEvent(false, eventLibrary, mapLibrary, itemLibrary);
            }
        });
        // 行为管理 -> 查看行为
        jMenu8.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEventAndActionTable(false, null, actionLibrary);
            }
        });
        // 行为管理 -> 添加行为
        jMenu8.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditAction(true, actionLibrary);
            }
        });
        // 行为管理 -> 编辑行为
        jMenu8.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditAction(false, actionLibrary);
            }
        });
        // 攻击动画 -> 查看动画
        jMenu9.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCTableAnimation(animationLibrary, iconImage);
            }
        });
        // 攻击动画 -> 添加动画
        jMenu9.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditAnimation(true, animationLibrary, FCGMaster.this);
            }
        });
        // 攻击动画 -> 编辑动画
        jMenu9.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditAnimation(false, animationLibrary, FCGMaster.this);
            }
        });
        // 剧情系统 -> 查看剧情
        jMenu10.getjMenuItem(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        // 剧情系统 -> 添加剧情
        jMenu10.getjMenuItem(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditPlot(true, plotLibrary);
            }
        });
        // 剧情系统 -> 编辑剧情
        jMenu10.getjMenuItem(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditPlot(false, plotLibrary);
            }
        });
        jMenu11.getjMenuItem(1).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditMapAnimation(true, FCGMaster.this);
            }
        });
        jMenu11.getjMenuItem(2).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FCEditMapAnimation(false, FCGMaster.this);
            }
        });
        /******************** 窗体上方菜单栏监听事件END ********************/

        /***************** 窗口监听器START *****************/
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
                try {
//					BufferedWriter wr = new BufferedWriter(new FileWriter(new File("./data/blocklist.vw")));
//					for (int i = 0; i < blockLibrary.getBlockNum(); i++) {
//						wr.write(blockLibrary.getBlockNameOfID(i) + ".vw");
//						wr.newLine();
//					}
//					wr.close();
//					wr = new BufferedWriter(new FileWriter(new File("./data/maplist.vw")));
//					for (int i = 0; i < mapLibrary.getMapNum(); i++) {
//						wr.write(mapLibrary.getMapNameOfID(i) + ".vw");
//						wr.newLine();
//					}
//					wr.close();
                    BufferedWriter wr = new BufferedWriter(new FileWriter(new File("./data/characterlist.vw")));
//                    for (int i = 0; i < characterLibrary.getCharacterNewID(); i++) {
//                        wr.write(characterLibrary.getCharacterNameOfID(i) + ".vw");
//                        wr.newLine();
//                    }
//                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/eventlist.vw")));
                    for (int i = 0; i < eventLibrary.getEventNum(); i++) {
                        wr.write(eventLibrary.getEventNameOfID(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/actionlist.vw")));
                    for (int i = 0; i < actionLibrary.getActionNum(); i++) {
                        wr.write(actionLibrary.getActionNameOfID(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/animationlist.vw")));
                    for (int i = 0; i < animationLibrary.getAnimationNum(); i++) {
                        wr.write(animationLibrary.getIDOfNum(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/itemlist.vw")));
                    for (int i = 0; i < itemLibrary.getItemNewID(); i++) {
                        wr.write(itemLibrary.getNameOfNum(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/monsterlist.vw")));
                    for (int i = 0; i < monsterLibrary.getMonsterNewID(); i++) {
                        wr.write(monsterLibrary.getMonsterNameOfNum(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/plotlist.vw")));
                    for (int i = 0; i < plotLibrary.getPlotNum(); i++) {
                        wr.write(plotLibrary.getIdOfNum(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    wr = new BufferedWriter(new FileWriter(new File("./data/tanklist.vw")));
                    for (int i = 0; i < tanksLibrary.getNewTankID(); i++) {
                        wr.write(tanksLibrary.getTankNameOfID(i) + ".vw");
                        wr.newLine();
                    }
                    wr.close();
                    System.exit(0);
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }
        });
        /***************** 窗口监听器END *****************/

        // 设置以上内容均为可视化
        setVisible(true);
    }

    /**
     * TODO 暂未使用
     *
     * @param num
     */
    private void CFAction(int num) {
        selectCF = (selectCF == num) ? -1 : num;
        for (int i = 0; i < cf.length; i++) {
            cf[i].setSelected(i == selectCF);
        }
    }

    protected void setBlockLibrary(FCBlockLibrary blockl) {
        this.blockLibrary = blockl;
    }


    //创建小地图
    private void buildSmallMapImg() {
        Schedulers.io().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                for(FCMap map: mapLibrary.getMapList()){
                   // System.out.println("创建--"+map.getName()+"缩略图");
                   // buildMapImg(map);
                }
                System.out.println("================END============");
            }
        });
    }

    private void buildMapImg(FCMap map){
        int worldW = map.getMapWidth();
        int worldH = map.getMapHeight();

        for (int num = 0; num < 1; num++) {
            BufferedImage out_1 = new BufferedImage(8 * (worldH+1), 8 * (worldW+1), BufferedImage.TYPE_USHORT_565_RGB);
            Graphics g = out_1.getGraphics();
            for (int n = 0; n < worldW; n++) {
                for (int m = 0; m < worldH; m++) {
                    int rN =  n;
                    int rM = m;
                    String blockname = map.getUnplaceNameOfCoor(rN, rM);
                    if (!blockname.equals("v")&&!blockname.equals("null")) {
                        Image[][] unplaceim = blockLibrary.getBlockOfID(blockname).getBlockImage();
                        for (int i = unplaceim.length - 1; i > -1; i--)
                            for (int j = 0; j < unplaceim[i].length; j++) {
                                Image u = unplaceim[i][j];
                                int celly = n + j;
                                int cellx = m - (unplaceim.length - i - 1);
                                if(u !=null ) {
                                    for(int tryT = 0; tryT<10; tryT++) {
                                        boolean ret = g.drawImage(u, cellx * 8+4, celly * 8+4, 8, 8, null);
                                        if (!ret) {
                                            //System.out.println("失败绘画1");
                                            try {
                                                Thread.sleep(200);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }else {
                                            break;
                                        }
                                    }
                                }
                            }
                    }

                    blockname = map.getPlaceNameOfCoor(rN, rM);
                    if (!blockname.equals("v")&&!blockname.equals("null")) {
                        Image[][] placeim = blockLibrary.getBlockOfID(blockname).getBlockImage();
                        for (int i = placeim.length - 1; i > -1; i--)
                            for (int j = 0; j < placeim[i].length; j++) {
                                Image u = placeim[i][j];
                                int celly = n + j;
                                int cellx = m - (placeim.length - i - 1);
                                if (u != null) {
                                    for(int tryT = 0; tryT<10; tryT++) {
                                        boolean ret = g.drawImage(u, cellx * 8+4, celly * 8+4, 8, 8, null);
                                        if (!ret) {
                                          //  System.out.println("失败绘画2");
                                            try {
                                                Thread.sleep(200);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }else {
                                            break;
                                        }
                                    }
                                }
                            }
                    }
                }
            }
            writeImageLocal("./material/png/mapImg/" + map.getName() + ".png", out_1);
            g.dispose();
        }
    }

    /**
     * 生成新图片到本地
     */
    public void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "png", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
